package com.example.nick.bodyapplication

import android.content.Intent
import android.media.MediaCodecList
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.ui.unit.dp
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.example.nick.bodyapplication.anim.AnimaActivity
import com.example.nick.bodyapplication.ble.BleActivity
import com.example.nick.bodyapplication.compose.ComposeActivity
import com.example.nick.bodyapplication.jetpack.NavActivity
import com.example.nick.bodyapplication.log.LogActivity
import com.example.nick.bodyapplication.notification.NotifcationActivity
import com.example.nick.bodyapplication.number.NumberActivity
import com.example.nick.bodyapplication.recycler.List2Activity
import com.example.nick.bodyapplication.recycler.ListActivity
import com.example.nick.bodyapplication.timerule.TimePart
import com.example.nick.bodyapplication.xml.XmlActivity
import java.util.*

class RouterActivity : AppCompatActivity() {

    var timelist = mutableListOf<TimePart>()
    var timelist2 = mutableListOf<TimePart>()
    var timelistResult = mutableListOf<TimePart>()
    var timelistTemp = mutableListOf<TimePart>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_router)
//        initTimePart()
//        dealDoubleList()
        testKt()
        testMediaCodec()
        testGlide()
        testTextDrawable()
    }

    lateinit var tvSel30: TextView
    lateinit var tvSel60: TextView
    private fun testTextDrawable() {
        tvSel30 = findViewById<TextView>(R.id.tvSel30)
        tvSel60 = findViewById<TextView>(R.id.tvSel60)
        tvSel30.setOnClickListener { deal30or60(true) }
        tvSel60.setOnClickListener { deal30or60(false) }
    }

    fun deal30or60(sel30: Boolean) {
        val drawableSel = resources.getDrawable(R.drawable.checkcircle_sel)
        drawableSel.setBounds(0, 0, drawableSel.minimumWidth, drawableSel.minimumHeight)
        val drawableUnSel = resources.getDrawable(R.drawable.checkcircle_unsel)
        drawableUnSel.setBounds(0, 0, drawableUnSel.minimumWidth, drawableUnSel.minimumHeight)
        if (sel30) {
            tvSel30.setCompoundDrawables(drawableSel, null, null, null)
            tvSel60.setCompoundDrawables(drawableUnSel, null, null, null)
        } else {
            tvSel30.setCompoundDrawables(drawableUnSel, null, null, null)
            tvSel60.setCompoundDrawables(drawableSel, null, null, null)
        }
    }

    private fun testGlide() {
        val ivPic = findViewById<ImageView>(R.id.ivPic)
        val roundRect = RoundedCornerShape(8.dp)
//        val options = RequestOptions()
//            .transforms(CenterCrop(), RoundedCorners(12))
//            .placeholder(R.drawable.shape_corner_white_8)
//            .diskCacheStrategy(DiskCacheStrategy.ALL)

        val options = RequestOptions()
//            .centerCrop()
            .transform(RoundedCorners(12))
            .placeholder(R.color.teal_200)
            .diskCacheStrategy(DiskCacheStrategy.ALL)

        val url="https://www.baidu.com/img/bd_logo1.png?where=super"
        val urlMk="http://mkadmin-cn.mkcziot.com/pic/16/68bb22c84e9f7aa82f540c20c689e488.jpg"
        Glide.with(this)/*在哪个页面显示*/
            .load(url)
            .apply(options)
            .into(ivPic)
//        Glide.with(this)
//            .load("http://mkadmin-cn.mkcziot.com/pic/16/68bb22c84e9f7aa82f540c20c689e488.jpg")
//            .apply(options)
//            .into(ivPic)
    }

    fun testMediaCodec() {
        for (i in 0..MediaCodecList.getCodecCount()-1) {
            val mediaCodecInfo = MediaCodecList.getCodecInfoAt(i)
            //OMX.rk.video_decoder.hevc
            //OMX.google.hevc.decoder
            if (mediaCodecInfo.name.contains("hevc") && !mediaCodecInfo.isEncoder) {
                Log.i("media", "mediaCodecInfo[$i]:${mediaCodecInfo.name}, isEncoder:${mediaCodecInfo.isEncoder}")
                Log.i("media", "supportedTypes:${mediaCodecInfo.supportedTypes[0]}")
                val codecCapabilities = mediaCodecInfo.getCapabilitiesForType("video/hevc")
                codecCapabilities.profileLevels.forEach {
                    Log.i("media", "profileLevels profile:${it.profile}, level:${it.level}")
                }
                val videoCapabilities = codecCapabilities.videoCapabilities
                Log.i("media", "supportedWidths upper:${videoCapabilities.supportedWidths.upper}, lower:${videoCapabilities.supportedHeights.lower}")
                Log.i("media", "supportedHeights upper:${videoCapabilities.supportedHeights.upper}, lower:${videoCapabilities.supportedHeights.lower}")
            }
        }

    }

    @ExperimentalAnimationApi
    fun toCompose(view: View) {
        startActivity(Intent(this, ComposeActivity::class.java))
    }

    fun toAlarmView(view: View) {
        startActivity(Intent(this, MyViewActivity::class.java))
    }

    fun toWeekDayView(view: View) {
        startActivity(Intent(this, WeekDayActivity::class.java))
    }

    fun toListView(view: View) {
//        startActivity(Intent(this, ListActivity::class.java))
        startActivity(Intent(this, List2Activity::class.java))
    }

    fun toNav(view: View) {
        startActivity(Intent(this, NavActivity::class.java))
    }

    fun toBle(view: View) {
        startActivity(Intent(this, BleActivity::class.java))
    }

    fun toNumberActivity(view: View) {
        startActivity(Intent(this, NumberActivity::class.java))
    }

    fun toFlowActivity(view: View) {
        startActivity(Intent(this, FlowActivity::class.java))
    }

    fun toGreenDaoActivity(view: View) {
        startActivity(Intent(this, GreenDaoActivity::class.java))
    }

    fun toNotifcationActivity(view: View) {
        startActivity(Intent(this, NotifcationActivity::class.java))
    }

    fun initTimePart() {
        timelist.add(TimePart(0, 5, 1))
        timelist.add(TimePart(6, 10, 1))
        timelist.add(TimePart(10, 20, 1))
        timelist.add(TimePart(20, 30, 1))
        timelist.add(TimePart(31, 41, 1))
        timelist.add(TimePart(41, 51, 2))
    }

    fun isSameColor(timePart0: TimePart, timePart1: TimePart): Boolean {
        if (timePart0.endTime != timePart1.startTime) {
            return false
        }
        if (timePart0.type == timePart1.type) {
            return true
        }
        return false
    }

    fun dealDoubleList() {
        for (i in timelist.indices) {
            if (i == 0) {
                timelistResult.add(timelist[0])
                continue
            }
            val lastIndex = timelistResult.size - 1
            val timePart0 = timelistResult[lastIndex]
            val timePart1 = timelist[i]
            if (isSameColor(timePart0, timePart1)) {
                timePart0.endTime = timePart1.endTime
            } else {
                timelistResult.add(timePart1)
            }
        }
        Log.i("time", "timelist:$timelist \n timelistResult:$timelistResult")
    }
    class AAA {
        var bbb: String? = null
    }
    fun testKt() {
//        var aaa:AAA = AAA()
//        var apType = false
//        aaa?.bbb?.contains("ccc").let {
//            apType = it?:false
//        }
//
//        apType = aaa?.bbb?.contains("ccc")?:false
//        aaa?:false
//        apType = (aaa?:false)
//        Log.i("ktkt", "apType:$apType")
    }

    fun toAnimaActivity(view: View) {
        startActivity(Intent(this, AnimaActivity::class.java))
    }

    fun toXmlActivity(view: View) = startActivity(Intent(this, XmlActivity::class.java))
    fun toLogActivity(view: View) = startActivity(Intent(this, LogActivity::class.java))
}